<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\OrdersCrontabRepository;
use App\Repository\Models\OrdersCrontab;

/**
 * Class OrdersCrontabRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class OrdersCrontabRepositoryEloquent extends BaseRepository implements OrdersCrontabRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'status',
        'order_id',
        'created_at'=>'between',
        'expired_at'=> 'between',
    ];
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return OrdersCrontab::class;
    }

    public function cancelDone($order_id)
    {
        $ret = $this->query()
            ->where(['order_id' => $order_id, 'type' => OrdersCrontab::TYPE_CANCEL, 'status' => OrdersCrontab::STATUS_NONE])
            ->update(['status' => OrdersCrontab::STATUS_DONE]);
        return $ret ? true : false;
    }

    public function affirmDone($order_id)
    {
        $ret = $this->query()
            ->where(['order_id' => $order_id, 'type' => OrdersCrontab::TYPE_AFFIRM, 'status' => OrdersCrontab::STATUS_NONE])
            ->update(['status' => OrdersCrontab::STATUS_DONE]);
        return $ret ? true : false;
    }
}
